package com.i2works.smartluolong.api.controller.user;

import com.google.common.collect.Lists;
import com.i2works.smartluolong.api.utils.BaseModule;
import com.i2works.smartluolong.utils.base.*;
import com.i2works.smartluolong.utils.common.WebUtil;
import com.i2works.smartluolong.utils.entity.R;
import com.i2works.smartluolong.utils.entity.Result;
import com.i2works.smartluolong.utils.exception.ServiceException;
import com.i2works.smartluolong.web.entity.cart.CartReceiveDO;
import com.i2works.smartluolong.web.entity.cart.CartReceiveQuery;
import com.i2works.smartluolong.web.entity.company.CompanyApplyDO;
import com.i2works.smartluolong.web.entity.company.CompanyChangeDO;
import com.i2works.smartluolong.web.entity.flow.FlowInfoDO;
import com.i2works.smartluolong.web.entity.flow.FlowInfoQuery;
import com.i2works.smartluolong.web.entity.flow.FlowProcessDO;
import com.i2works.smartluolong.web.entity.merchant.MerchantApplyDO;
import com.i2works.smartluolong.web.service.cart.ICartReceiveService;
import com.i2works.smartluolong.web.service.company.ICompanyApplyService;
import com.i2works.smartluolong.web.service.company.ICompanyChangeService;
import com.i2works.smartluolong.web.service.flow.IFlowInfoService;
import com.i2works.smartluolong.web.service.flow.IFlowProcessService;
import com.i2works.smartluolong.web.service.merchant.IMerchantApplyService;
import com.xiaoleilu.hutool.util.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户业务控制层
 *
 * @author Liu Yutao <koal@vip.qq.com>
 * @date 2018/3/22 15:25
 */
@RestController
@RequestMapping("/user")
public class UserServiceController extends BaseModule {


    private final ICartReceiveService cartReceiveService;
    private final IFlowInfoService flowInfoService;
    private final IFlowProcessService flowProcessService;
    private final ICompanyApplyService companyApplyService;
    private final ICompanyChangeService companyChangeService;
    private final IMerchantApplyService merchantApplyService;

    @Autowired
    public UserServiceController(ICartReceiveService cartReceiveService, IFlowInfoService flowInfoService, IFlowProcessService flowProcessService, ICompanyApplyService companyApplyService, ICompanyChangeService companyChangeService, IMerchantApplyService merchantApplyService) {
        this.cartReceiveService = cartReceiveService;
        this.flowInfoService = flowInfoService;
        this.flowProcessService = flowProcessService;
        this.companyApplyService = companyApplyService;
        this.companyChangeService = companyChangeService;
        this.merchantApplyService = merchantApplyService;
    }

    /**
     * 办件列表
     */
    @GetMapping("/transactList")
    public Result transactList(Integer userId, Long limit, Long offset) {
        try {
            FlowInfoQuery query = new FlowInfoQuery();
            query.setUserId(userId);
            query.setLimit(limit);
            query.setOffset(offset);
            Integer queryCount = flowInfoService.queryCount(query);
            List<FlowInfoDO> resultList = Lists.newArrayList();
            if (queryCount != 0) {
                List<FlowInfoDO> queryList = flowInfoService.queryList(query);
                if (CollectionUtil.isNotEmpty(queryList)) {
                    for (FlowInfoDO flowInfoDO : queryList) {
                        resultList.add(getItemStatusText(flowInfoDO));
                    }
                }
            }
            return R.success(resultList, queryCount);
        } catch (ServiceException e) {
            logger.error(e.getMessage());
            return R.error();
        }
    }


    /**
     * 证件领取
     */
    @GetMapping("/receive")
    public Result receiveApi(@ModelAttribute CartReceiveQuery cartReceiveQuery) {
        List<CartReceiveDO> resultList = Lists.newArrayList();
        try {
            cartReceiveQuery.setIssueStatus(1);
            cartReceiveQuery.setItemUserId(getUserId());
            Integer count = cartReceiveService.queryCount(cartReceiveQuery);
            if (count > 0) {
                List<CartReceiveDO> queryList = cartReceiveService.queryList(cartReceiveQuery);
                if (CollectionUtil.isNotEmpty(queryList)) {
                    for (CartReceiveDO cartReceiveDO : queryList) {
                        String typeValue = WebUtil.itemTypeValue(cartReceiveDO.getItemType());
                        cartReceiveDO.setItemTypeValue(typeValue);
                        resultList.add(cartReceiveDO);
                    }
                }
            }
            return R.success(resultList, count);
        } catch (ServiceException e) {
            logger.error("", e.getMessage());
            return R.error(MsgEnum.SYSTEM_ERROR.getMsg());
        }
    }


    /**
     * 作废
     */
    @GetMapping("/invalid/action")
    public Result invalid(@ModelAttribute FlowInfoDO flowInfoDO, String userName) {
        try {
            FlowInfoQuery query = new FlowInfoQuery();
            query.setUserId(flowInfoDO.getUserId());
            query.setItemId(flowInfoDO.getItemId());
            query.setItemType(flowInfoDO.getItemType());
            Integer queryCount = flowInfoService.queryCount(query);
            if (queryCount > 0) {
                flowInfoDO.setItemStatus(CompanyStatusEnum.STATUS_8.getKey());
                flowInfoService.update(flowInfoDO);
                //更新进度表
                FlowProcessDO flowProcessDO = new FlowProcessDO();
                flowProcessDO.setItemId(flowInfoDO.getItemId());
                flowProcessDO.setItemType(flowInfoDO.getItemType());
                flowProcessDO.setItemTitle(flowInfoDO.getItemName());
                flowProcessDO.setAuditStatus(CompanyStatusEnum.STATUS_8.getKey());
                flowProcessDO.setMessage("用户《" + userName + "》提交" + CompanyStatusEnum.STATUS_8.getText());
                flowProcessDO.setUpdateName(userName);
                flowProcessDO.setCreateName(userName);
                flowProcessService.save(flowProcessDO);
                //更新业务表
                String itemType = flowInfoDO.getItemType();

                FlowItemTypeEnum typeEnum = WebUtil.getEnum(FlowItemTypeEnum.class, flowInfoDO.getItemType(), null);
                if (null != typeEnum) {
                    switch (typeEnum) {
                        //工商
                        case COMPANY_APPLY:
                            CompanyApplyDO companyApplyDO = new CompanyApplyDO();
                            companyApplyDO.setId(flowInfoDO.getItemId());
                            companyApplyDO.setApplyStatus(CompanyStatusEnum.STATUS_8.getKey());
                            companyApplyService.update(companyApplyDO);
                            break;
                        case COMPANY_CHANGE:
                            break;
                        case COMPANY_RECORD:
                            CompanyChangeDO companyChangeDO = new CompanyChangeDO();
                            companyChangeDO.setId(flowInfoDO.getItemId());
                            companyChangeDO.setChangeStatus(CompanyStatusEnum.STATUS_8.getKey());
                            companyChangeService.update(companyChangeDO);
                            break;
                        //食药
                        case FOODDRUG_APPLY:
                            break;
                        case FOODDRUG_CHANGE:
                            break;
                        case FOODDRUG_CANCEL:
                            break;
                        //个体户
                        case MERCHANT_APPLY:
                            MerchantApplyDO merchantApplyDO = new MerchantApplyDO();
                            merchantApplyDO.setId(flowInfoDO.getItemId());
                            merchantApplyDO.setApplyStatus(MerchantStatusEnum.STATUS_5.getKey());
                            merchantApplyService.update(merchantApplyDO);
                            break;
                        case MERCHANT_CHANGE:
                            break;
                        case MERCHANT_RECORD:
                            break;
                    }
                }

                return R.success();
            }
        } catch (ServiceException e) {
            logger.error(e.getMessage());
            return R.error(MsgEnum.FIND_ERROR.getMsg());
        }
        return R.error(MsgEnum.EDIT_ERROR.getMsg());
    }


    /**
     * 获取项目状态字符串，并返回工作流信息实体
     */
    private FlowInfoDO getItemStatusText(FlowInfoDO flowInfoDO) {
        FlowItemTypeEnum[] values = FlowItemTypeEnum.values();
        List<String> companyList = Lists.newArrayList(FlowItemTypeEnum.COMPANY_APPLY.getKey(), FlowItemTypeEnum.COMPANY_CHANGE.getKey(), FlowItemTypeEnum.COMPANY_RECORD.getKey());
        List<String> merchantList = Lists.newArrayList(FlowItemTypeEnum.MERCHANT_APPLY.getKey(), FlowItemTypeEnum.MERCHANT_CHANGE.getKey(), FlowItemTypeEnum.MERCHANT_RECORD.getKey());
        List<String> fooddrugList = Lists.newArrayList(FlowItemTypeEnum.FOODDRUG_APPLY.getKey(), FlowItemTypeEnum.FOODDRUG_CHANGE.getKey(), FlowItemTypeEnum.FOODDRUG_CANCEL.getKey());
        List<String> disabilityList = Lists.newArrayList(FlowItemTypeEnum.DISABILITY_APPLY.getKey(), FlowItemTypeEnum.DISABILITY_CHANGE.getKey(), FlowItemTypeEnum.DISABILITY_REAPPLY.getKey(), FlowItemTypeEnum.DISABILITY_CANCEL.getKey());
        String itemStatus;
        //公司
        if (companyList.contains(flowInfoDO.getItemType())) {
            itemStatus = WebUtil.itemStatus(flowInfoDO.getItemStatus(), CompanyStatusEnum.class);
            flowInfoDO.setItemStatusText(itemStatus);
        }
        //个体
        if (merchantList.contains(flowInfoDO.getItemType())) {
            itemStatus = WebUtil.itemStatus(flowInfoDO.getItemStatus(), MerchantStatusEnum.class);
            flowInfoDO.setItemStatusText(itemStatus);
        }
        //食药
        if (fooddrugList.contains(flowInfoDO.getItemType())) {
            itemStatus = WebUtil.itemStatus(flowInfoDO.getItemStatus(), FooddrugStatusEnum.class);
            flowInfoDO.setItemStatusText(itemStatus);
        }
        //残联
        if (disabilityList.contains(flowInfoDO.getItemType())) {
            itemStatus = WebUtil.itemStatus(flowInfoDO.getItemStatus(), DisabilityStatusEnum.class);
            flowInfoDO.setItemStatusText(itemStatus);
        }
        return flowInfoDO;
    }

}
